package SinglyList;

public class TermX implements Comparable<TermX>{

    //分别声明系数和指数
    protected int coef,xexp;

    public TermX() {
    }

    public TermX(int coef, int xexp) {
        this.coef = coef;
        this.xexp = xexp;
    }

    public boolean equals(TermX termX) {
        return (termX.coef==this.coef&&termX.xexp==this.xexp);
    }

    @Override
    public String toString() {
        if (xexp!=0){
            if (coef == 1) {
                return "x^" + this.xexp;
            } else if (coef == -1) {
                return "-x^" + this.xexp;
            } else {
                return this.coef + "x^" + this.xexp;
            }
        }else{
            return String.valueOf(coef);
        }
    }

    public void add(TermX termX){
        if (termX.xexp==this.xexp){
            this.xexp += termX.xexp;
        }
    }


    @Override
    public int compareTo(TermX termX) {
        if (this.xexp> termX.xexp){
            return 1;
        }else if (this.xexp< termX.xexp){
            return -1;
        }else return 0;
    }
}
